/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package processorUtilizationNB.impl;

import generalArchitecture.impl.ComputeBoxImpl;

import java.util.Collection;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.util.EDataTypeUniqueEList;

import processorUtilizationNB.ProcessorUtilisationNB;
import processorUtilizationNB.ProcessorUtilizationNBPackage;
import technicRMLL73.TechnicRMLL73;
import technicRMLL73.impl.TechnicRMLL73Impl;
import utils.UtilsModelInstance;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Processor Utilisation NB</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link processorUtilizationNB.impl.ProcessorUtilisationNBImpl#getPeriod <em>Period</em>}</li>
 *   <li>{@link processorUtilizationNB.impl.ProcessorUtilisationNBImpl#getWcet <em>Wcet</em>}</li>
 *   <li>{@link processorUtilizationNB.impl.ProcessorUtilisationNBImpl#getU <em>U</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class ProcessorUtilisationNBImpl extends ComputeBoxImpl implements ProcessorUtilisationNB {
	/**
	 * The cached value of the '{@link #getPeriod() <em>Period</em>}' attribute list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getPeriod()
	 * @generated
	 * @ordered
	 */
	protected EList<Integer> period;

	/**
	 * The cached value of the '{@link #getWcet() <em>Wcet</em>}' attribute list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getWcet()
	 * @generated
	 * @ordered
	 */
	protected EList<Integer> wcet;

	/**
	 * The default value of the '{@link #getU() <em>U</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getU()
	 * @generated
	 * @ordered
	 */
	protected static final double U_EDEFAULT = 0.0;

	/**
	 * The cached value of the '{@link #getU() <em>U</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getU()
	 * @generated
	 * @ordered
	 */
	protected double u = U_EDEFAULT;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected ProcessorUtilisationNBImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return ProcessorUtilizationNBPackage.Literals.PROCESSOR_UTILISATION_NB;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<Integer> getPeriod() {
		if (period == null) {
			period = new EDataTypeUniqueEList<Integer>(Integer.class, this, ProcessorUtilizationNBPackage.PROCESSOR_UTILISATION_NB__PERIOD);
		}
		return period;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<Integer> getWcet() {
		if (wcet == null) {
			wcet = new EDataTypeUniqueEList<Integer>(Integer.class, this, ProcessorUtilizationNBPackage.PROCESSOR_UTILISATION_NB__WCET);
		}
		return wcet;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public double getU() {
		return u;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setU(double newU) {
		double oldU = u;
		u = newU;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, ProcessorUtilizationNBPackage.PROCESSOR_UTILISATION_NB__U, oldU, u));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public void compute() {

		/* Compute the bound */
		this.u = 0.0;

		for (int i = 0 ; i<this.period.size() ; i++) {

			u = u + (1.0 * this.wcet.get(i) ) / this.period.get(i);
		}

		/* look for instance of technicRMLL73 */
		TechnicRMLL73 t_rm = (TechnicRMLL73)UtilsModelInstance.lookForComputeBox(this, TechnicRMLL73Impl.class);
		
		/* Instance doesn't exist */
		if (t_rm == null) {

			/* Create instance, add to general list, to children list and compute */
			t_rm = UtilsModelInstance.createTechnicRMLL73(null, this);
			UtilsModelInstance.linkComputeBox(this, t_rm, false);
			
		}

		/* Instance exists */
		else {

			/* Set Branch Graph */
			UtilsModelInstance.linkComputeBox(this, t_rm, false);

			/* Update attributes and compute */
			t_rm.setPU(this);
		}

		t_rm.compute();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public void setWcets(EList<Integer> l_wcets) {

		this.wcet = l_wcets;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public void setPeriods(EList<Integer> l_periods) {

		this.period = l_periods;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
		case ProcessorUtilizationNBPackage.PROCESSOR_UTILISATION_NB__PERIOD:
			return getPeriod();
		case ProcessorUtilizationNBPackage.PROCESSOR_UTILISATION_NB__WCET:
			return getWcet();
		case ProcessorUtilizationNBPackage.PROCESSOR_UTILISATION_NB__U:
			return new Double(getU());
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
		case ProcessorUtilizationNBPackage.PROCESSOR_UTILISATION_NB__PERIOD:
			getPeriod().clear();
			getPeriod().addAll((Collection<? extends Integer>)newValue);
			return;
		case ProcessorUtilizationNBPackage.PROCESSOR_UTILISATION_NB__WCET:
			getWcet().clear();
			getWcet().addAll((Collection<? extends Integer>)newValue);
			return;
		case ProcessorUtilizationNBPackage.PROCESSOR_UTILISATION_NB__U:
			setU(((Double)newValue).doubleValue());
			return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
		case ProcessorUtilizationNBPackage.PROCESSOR_UTILISATION_NB__PERIOD:
			getPeriod().clear();
			return;
		case ProcessorUtilizationNBPackage.PROCESSOR_UTILISATION_NB__WCET:
			getWcet().clear();
			return;
		case ProcessorUtilizationNBPackage.PROCESSOR_UTILISATION_NB__U:
			setU(U_EDEFAULT);
			return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
		case ProcessorUtilizationNBPackage.PROCESSOR_UTILISATION_NB__PERIOD:
			return period != null && !period.isEmpty();
		case ProcessorUtilizationNBPackage.PROCESSOR_UTILISATION_NB__WCET:
			return wcet != null && !wcet.isEmpty();
		case ProcessorUtilizationNBPackage.PROCESSOR_UTILISATION_NB__U:
			return u != U_EDEFAULT;
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public String toString() {
		if (eIsProxy()) return super.toString();

		StringBuffer result = new StringBuffer(super.toString());
		result.append(" (period: ");
		result.append(period);
		result.append(", wcet: ");
		result.append(wcet);
		result.append(", u: ");
		result.append(u);
		result.append(')');
		return result.toString();
	}

} //ProcessorUtilisationNBImpl
